<template>
	<view class="">
		<view class="images">
			<view class="">
				<image src="../../static/images/tongyuan.png.jpg" mode=""></image>
			</view>
		</view>

		<view class="btn">
			<button class="bottom bg-green" open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGotUserInfo">点击登录</button>
		</view>

	</view>
</template>
<script>
	var rawData, _that;
	export default {
		data() {
			return {
				uninid: null,
				sesskey: null,
				opid: null,
				code: '',
				openids: '',
				id: '',
				userInfo: [],
			}
		},
		onLoad() {
			_that = this;
			setTimeout(function() {
				_that.loading = true
			}, 500)

		},
		onLaunch() {
			// 获取用户信息

		},
		methods: {

			onGotUserInfo: function(e) {
				var that = this;
				rawData = e.detail.rawData;
				if (e.detail.iv) {
					_that.userInfo = e.detail.userInfo;
					//获取用户openid
					uni.login({
						success: res => {
							//code值(5分钟失效)
							this.request.selectOppenId({
								code: res.code,
								appid: 'wx23df356f1b08842e',
								secret: '61021a7bd91b1fba54cb7fc3b1a9b893',
								state: 1
							}).then(res => {
								this.opid = res.appOppenId
								this.sesskey = res.sessionKey
								this.sesskeyo(this.sesskey)
								uni.setStorage({
									key: 'user',
									data: this.opid
								})
								// B页面
								// uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
								// 	delta: 1
								// });
							})
						},
					});
					uni.showToast({
						title: "登录成功",
					})

					this.logtext = !this.logtext
				} else {
					uni.showToast({
						title: '用户拒绝授权',
						icon: 'none'
					});
				}
			},
			sesskeyo(sesskeys) {
				var that = this;
				wx.getUserInfo({
					success: function(res) {
						let WXBizDataCrypt = require('./js/WXBizDataCrypt.js')
						let appId = 'wx23df356f1b08842e'
						let sessionKey = sesskeys
						let encryptedData = res.encryptedData
						let iv = res.iv
						let pc = new WXBizDataCrypt(appId, sessionKey)
						let data = pc.decryptData(encryptedData, iv)
						let unionid = data.unionId
						uni.setStorage({
							key: 'untid',
							data: unionid
						})
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						let nowPage = pages[pages.length - 1]; //当前页页面实例
						let prevPage = pages[pages.length - 2]; //上一页页面实例
						uni.navigateBack();
						that.add(unionid)
					}
				})
			},
			EmojiExchangeString(str) {
				var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则 
				str = str.replace(patt, function(char) {
					var H, L, code;
					if (char.length === 2) {
						H = char.charCodeAt(0); // 取出高位 
						L = char.charCodeAt(1); // 取出低位 
						code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法 
						return "&#" + code + ";";
					} else {
						return char;
					}
				});
				return str;
			},
			add(uninid) {
				let name = this.EmojiExchangeString(this.userInfo.nickName)
				this.request.add({
					name: name,
					logUrl: this.userInfo.avatarUrl,
					id: uninid,
					appOppenId: this.opid
				}).then(res => {})
			},
		}
	}
</script>

<style lang="scss">
	.images {
		display: flex;
		justify-content: center;
		margin-top: 200rpx;

		image {
			width: 100rpx;
			height: 100rpx;
		}
	}

	.btn {
		margin-top: 300rpx;

		.bottom {
			width: 300rpx;
			height: 80rpx;
			line-height: 80rpx;
		}
	}
</style>
